<template>
  <div>
    <el-card class="card">
      <el-descriptions title="成绩录入" direction="vertical" :column="4" size="large" :span="2" border></el-descriptions>
      <el-select  v-model="course"  class="m-2" placeholder="课程名" style="margin-left: 5%" @change="handleChangeCourse">
        <el-option v-for="item in coursesList" :key="item.id" :label="item.courseName" :value="item.id"/>
      </el-select>
<!--      <el-button @click="findStuNameList(courseId)" type="primary" style="margin-left: 5px;width: 60px;"><el-icon style="font-size: 20px;"><Search /></el-icon>搜索</el-button>-->

      <el-table class="courses-table" :data="stusCourseList" border>
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="grade" label="年级" />
        <el-table-column prop="score" label="成绩" >
          <template #default="scope">
            <span v-if="scope.row.score !== -1">{{ scope.row.score }}</span>
            <span v-else style="color: #888">无</span>
          </template>
        </el-table-column>
        <el-table-column prop="control" label="操作" >
          <template #default="scope">
            <el-button @click="openDialogFormVisible(scope.row.chooseCourseId)">录入成绩</el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      <el-pagination background layout="prev, pager, next" :total="100" style="margin-left: 32%;margin-top: 10px"/>-->
    </el-card>

    <el-dialog v-model="dialogFormVisible" title="请输入分数" style="width: 30%; margin-top: 20%" >
      <el-form>
        <el-form-item label="分数:" style="width: 40%; margin-left: 20%">
          <el-input v-model="input" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="giveScore">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue'
import { useRouter } from 'vue-router'
import {ChooseCourseItem, CourseItem, StudentItem, StusCourseItem, StusCourseList} from "@/api/teacher/type";
import {addScore, findStuNameList, getAllCourses, stusScoreByTeacher} from "@/api/teacher";
import {useTeacherStoreHook} from "@/store/modules/teacher";
import {GET_TEACHER} from "@/utils/teacherInfo";
import {chooseCourse} from "@/api/student";
import {ElMessage} from "element-plus";
const router = useRouter()
const useStore = useTeacherStoreHook()
const course = ref()
const coursesList = ref<CourseItem[]>()
const studentsList = ref<StudentItem>()
const stusCourseList = ref<StusCourseList[]>()
const reqStuCourse = reactive<StusCourseItem>({
  courseName: '',
  id: 1,
  teacherId: 1
})
const input = ref()

onMounted(async()=>{
  await getAllTeacherCourses()
})
const handleChangeCourse = async (newCourseValue: number) => {
  course.value = newCourseValue
  reqStuCourse.id = course.value
  await checkStusList()

}
//查看所授课程
const getAllTeacherCourses = async () => {
  try {
    const {data} =await getAllCourses(GET_TEACHER().id)
    coursesList.value = data
    console.log(coursesList.value)
  } catch (error) {
    await Promise.reject(error)
  }
}

//切换授课学生名单
const checkStusList = async () => {
  try {
    const {data} = await stusScoreByTeacher(reqStuCourse)
    stusCourseList.value = data
  } catch (error){
    await Promise.reject(error)
  }
}

//显示录入成绩
const dialogFormVisible = ref(false)
const openDialogFormVisible = async (id:number) => {
  reqStuCourse.value = id
  dialogFormVisible.value = true

}
//确认录入成绩
const giveScore = async () => {
  try {
    const res = await addScore(reqStuCourse.value, input.value)
    if (res.code === 200) {
      ElMessage({
        message: res.data,
        type: 'success',
      })
    }
    else {
      ElMessage({
        message: "res.message",
        type: 'info',
      })
    }
    dialogFormVisible.value = false
    await checkStusList()
  } catch (error) {
    await Promise.reject(error)
  }
}


</script>

<style scoped>
.card {
  margin: 1% auto 0;
  width: 90%;
  opacity: 0.9;
}
.courses-table {
  width: 90%;
  margin-top: 20px;
  margin-left: 5%;
}
</style>
